// xc_lshop/integralmall/luckdraw/reword/myprice.js
var actbill = require('../../model/integral_luckdraw')
var actprobill = require('../../model/integralpro_contents')

var app = getApp();

Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],
  
  /**
   * 页面的初始数据
   */
  data: {
    navstatus:"0"
  },
  onnavChange(e){
    var that=this;
    this.setData({
      navstatus:e.detail.name
    })
    that.getpagelist(that, 1)
  },
  xcloaddata: function (options, config) {
   
    var updateconfig={};
    if (config.prothem && config.prothem.detail) {
      var detailconfig = config.prothem.detail.proconfig;  
      var customStyle = config.prothem.detail.customStyle;
      var popconfig = config.prothem.popup.proconfig;
      var popCustomStyle = config.prothem.popup.customStyle;
      updateconfig['detailconfig'] = detailconfig
   
      updateconfig['customStyle'] = customStyle
      updateconfig['popconfig'] = popconfig
      updateconfig['popCustomStyle'] = popCustomStyle
    }
    this.setData(updateconfig)
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {};
      }
      if(this.data.navstatus){
        strwhere['receive']=this.data.navstatus
      }

      that.setData({ loading: true, modellist: [] });
    }
    // that.setData({ isload:true });

    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};

      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        }
        else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
     
      that.setData(updatemdoel)
     
    });
    
  },
  afterlogin: function (options, config, userinfo) {
     this.setData({
       member:userinfo
     })
  },
  exchange(e){
    var id=e.currentTarget.dataset.id;
    var pro=e.currentTarget.dataset.pro;
    if(pro){
      this.buyop(pro,id)  
    }else{
      var that = this;
      actbill.exchange(id).then(res => {
      
        if (res['status'] == 1) {
          that.showmessage("兑换成功",'success');
          that.getpagelist(that, 1)
        }else{
          this.error(res['message'],false);
        }
      })
    }
   
  },
  buyop: function (xcmodel,rewordid) {
    if (!this.data.member.member_id) {
      this.login();
    }


    if(xcmodel.stock<=0){ 
      wx.showToast({ 
        icon:"none", 
        title: '产品已兑完 ', 
      }) 
      return; 
    }  
    if(xcmodel.guigetype==-1){
      var newmodel={};
      newmodel['rewordid']=rewordid
      newmodel['activityid']='0';
      newmodel['activityprice']='-1';
      newmodel['attrnameinfo']=[];
      newmodel['attrs']=[];
      newmodel['bpid']=-1;
      newmodel['cid']='0';
      newmodel['flag']=-1;
      newmodel['id']=xcmodel['id'];
      newmodel['integral']=0;
      newmodel['keyval']=xcmodel['id']+"_0_0_-1";
      newmodel['name']=xcmodel['name'];
      newmodel['num']=1;
      newmodel['pnum']=-2;
      newmodel['price']=xcmodel['price'];
      newmodel['pro_ident']=-1;
      newmodel['realprice']=0;
      newmodel['simg']=xcmodel['simg'];
      newmodel['type']=xcmodel['type'];
      newmodel['weight']=xcmodel['weight'];
    

      app.globalData.temcart = {};
      app.globalData.temcart["data"] = [newmodel]; 
      wx.navigateTo({
        url: '/' + app.globalData.xcsys.m + '/integralmall/paycore/payinfo?iscart=-1'
      })
      return;
    }

    var that = this
    var option = "";

      var option = {
          aid: 0,
          atype: 0,
          rewordid:rewordid
      }

    actprobill.showattrchose(that, xcmodel.id, null, 2, option)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    that.getpagelist(that, 1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    that.getpagelist(that, -1)
  },

  
})